package org.example.stopthreads;

/**
 * 最佳实践 catch方法的异常，优先选择抛 最顶层的  try 的位置就是停止的位置
 */
public class RightWayStopThreadInProd2 implements Runnable {

  public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(new RightWayStopThreadInProd2());
    thread.start();
    Thread.sleep(1);
    thread.interrupt();
  }

  @Override
  public void run() {
    while (true) {
      if (Thread.currentThread().isInterrupted()) {
        System.out.println("收集日志");
        return;
      }
      System.out.println("业务逻辑1");
      System.out.println("业务逻辑2");
      throwInMethod();
      System.out.println("业务逻辑3");
    }
  }

  private void throwInMethod() {
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      e.printStackTrace();
    }
  }
}
